/**
 *
 */
package de.cbf.cam.dsa.rcp.model.entities.items;

import java.util.Iterator;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;

import de.cbf.util.uuidheader.entities.EntityWeighted;
import de.cbf.util.uuidheader.entities.UUIDReference;
import de.cbf.util.uuidheader.util.EntityWeightedList;

/**
 * @author salter
 *
 */
@XmlAccessorType(XmlAccessType.FIELD)
public class ItemForProfessionAlternatives extends ItemForProfession {

	protected String description;

	/** used for choices with "OR", here will be the alternativeItems as ProfessionItems<br>
	 * in resolver just choose one of them<br>
	 */
	@XmlElement(name="alternateItem")
	protected EntityWeightedList alternativeItems;


	public ItemForProfessionAlternatives() {
		super();
	}

	public ItemForProfessionAlternatives(String description) {
		super();
		this.description = description;
	}

	protected void init() {
		super.init();
		alternativeItems = new EntityWeightedList();
	}

	public void addAlternativeItem(EntityWeighted alternative){
		//check if it contains an ItemForProfession
		if (alternative.getEntity() instanceof UUIDReference) {
			alternativeItems.add(alternative);
		}
	}

	public void addAlternativeItem(UUIDReference itemRef,Double weight){
		EntityWeighted entity = new EntityWeighted(itemRef,weight);
		alternativeItems.add(entity);
	}

	public void removeAlternativeItem(UUIDReference itemRef){
		Iterator<EntityWeighted> it = alternativeItems.iterator();
		while (it.hasNext()) {
			EntityWeighted entityWeighted = (EntityWeighted) it.next();
			if(itemRef.sameUUID(entityWeighted.getEntity())){
				//no need to cast because only uuids matter
				it.remove();
			}
		}
	}

	public EntityWeightedList getAlternativeItems() {
		return new EntityWeightedList(alternativeItems);
	}

}
